package com.uit.trip_.fragments;

import com.uit.trip_.R;
import com.uit.trip_.tab_activity.Tab_TripHomeActivity;
import com.uit.trip_.tab_activity.Tab_TripLocationActivity;
import com.uit.trip_.tab_activity.Tab_TripSearchActivity;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

/*
 * Contain TabHost to manage all application tab.
 */
public class HomeFragment extends Fragment {

	private TabHost tabHost;
	private View view;
	private View tab_home;
	private LocalActivityManager mLocal;

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		setUserVisibleHint(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		tab_home = inflater.inflate(R.layout.home_fragment, container, false);
		view = inflater.inflate(R.layout.home_fragment, container, false);
		tabHost = (TabHost) view.findViewById(R.id.tabhost);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mLocal = new LocalActivityManager(getActivity(), false);

		mLocal.dispatchCreate(savedInstanceState);
		tabHost.setup(mLocal);
		initViews();
		// Intent i = new Intent(getActivity(), TripMainActivity.class);
		// startActivity(i);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("duongnx", "onPause");
		// mLocal.dispatchPause(true);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("duongnx", "onResume123");

		mLocal.dispatchResume();

	}

	public void initViews() {

		Log.e("duongnx", "dduong");
		Resources res = getResources();
		TabHost.TabSpec tabSpec;
		Intent i;

		tabSpec = tabHost.newTabSpec("Location");
		i = new Intent(getActivity(), Tab_TripLocationActivity.class);
		tabSpec.setIndicator("", res.getDrawable(R.drawable.tab_location));
		tabSpec.setContent(i);
		// tabSpec.setContent(R.id.tab1);
		tabHost.addTab(tabSpec);

		tabSpec = tabHost.newTabSpec("Home");
		i = new Intent(getActivity(), Tab_TripHomeActivity.class);
		tabSpec.setIndicator("", res.getDrawable(R.drawable.tab_home));
		tabSpec.setContent(i);
		// tabSpec.setContent(R.id.tab2);

		tabHost.addTab(tabSpec);

		tabSpec = tabHost.newTabSpec("Search");
		i = new Intent(getActivity(), Tab_TripSearchActivity.class);
		tabSpec.setIndicator("", res.getDrawable(R.drawable.tab_search));
		tabSpec.setContent(i);
		// tabSpec.setContent(R.id.tab3);
		tabHost.addTab(tabSpec);

		tabHost.setCurrentTab(1);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Log.i("duongnx", "onActivtityResultFragment");
	}

}